/**
 * 文章相关请求模块
 */
import request from '@/utils/request'
import {
  ArticleCategoryParams,
  ArticleCategory,
  ArticleCategoryForm,
  ArticleParams,
  Article,
  ArticleRelation,
  ArticleForm
} from './types/article'

/**
 * 文章分类列表
 * @param params 查询参数
 * @returns 商品列表
 */
export const getArticleCategory = (params?: ArticleCategoryParams) => {
  return request<ArticleCategory[]>({
    method: 'GET',
    url: '/cms/category',
    params
  })
}

/**
 * 修改文章分类状态
 */
export const putArticleCategoryStatus = (id: number, status: number) => {
  return request({
    method: 'PUT',
    url: `/cms/category/set_status/${id}/${status}`
  })
}

/**
 * 删除文章分类
 */
export const deleteArticleCategory = (id: number) => {
  return request({
    method: 'DELETE',
    url: `/cms/category/${id}`
  })
}

/**
 * 新增文章分类
 */
export const postArticleCategory = (data: ArticleCategoryForm) => {
  return request({
    method: 'POST',
    url: '/cms/category',
    data
  })
}

/**
 * 编辑文章分类
 */
export const putArticleCategory = (data: ArticleCategoryForm, id: number) => {
  return request({
    method: 'PUT',
    url: `/cms/category/${id}`,
    data
  })
}

/**
 * 文章列表
 * @param params 查询参数
 * @returns 商品列表
 */
export const getArticle = (params?: ArticleParams) => {
  return request<{
    count: number
    list: Article[]
  }>({
    method: 'GET',
    url: '/cms/cms',
    params
  })
}

/**
 * 文章关联产品
 */
export const putCmsRelation = (data: ArticleRelation, id: number) => {
  return request({
    method: 'PUT',
    url: `/cms/cms/relation/${id}`,
    data
  })
}

/**
 * 取消文章关联产品
 */
export const putCmsUnRelation = (id: number) => {
  return request({
    method: 'PUT',
    url: `/cms/cms/unrelation/${id}`
  })
}

/**
 * 删除文章
 */
export const deleteArticle = (id: number) => {
  return request({
    method: 'DELETE',
    url: `/cms/cms/${id}`
  })
}

/**
 * 文章详情
 */
export const getArticleDetail = (id: number) => {
  return request<{
    info: Article
  }>({
    method: 'GET',
    url: `/cms/cms/${id}`
  })
}

/**
 * 新增或修改文章
 */
export const postArticle = (data: ArticleForm) => {
  return request({
    method: 'POST',
    url: '/cms/cms',
    data
  })
}
